package main

import (
	"fmt"
	"zinx/ziface"
	"zinx/znet"
)

type PingRouter struct {
	znet.BashRouter
}

// Test Handle
func (p *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call Router Handle")
	//先读取客户端的数据
	fmt.Println("recv from client : msgId=", request.GetMsgId(), ", data=", string(request.GetData()))
	//再回写ping...ping...ping
	if err := request.GetConnection().SendMsg(200, []byte("ping...ping...ping")); err != nil {
		fmt.Println("send msg err:", err)
	}
}

type HelloRouter struct {
	znet.BashRouter
}

// Test Handle
func (p *HelloRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call HelloRouter Handle")
	//先读取客户端的数据
	fmt.Println("recv from client : msgId=", request.GetMsgId(), ", data=", string(request.GetData()))
	//再回写Welcome to Zinx V0.6
	if err := request.GetConnection().SendMsg(201, []byte("Welcome to Zinx V0.6")); err != nil {
		fmt.Println("send msg err:", err)
	}
}

func main() {
	//1、创建一个server的句柄，使用Zinx的api
	s := znet.NewServer("[zinx V0.6]")

	//2、给当前Zinx框架添加自定义的router
	s.AddRouter(0, &PingRouter{})
	s.AddRouter(1, &HelloRouter{})

	//3、启动server
	s.Serve()
}
